package com.whfc.mach.manager.warn.impl;

import com.alibaba.fastjson.JSON;
import com.whfc.base.enums.AppModuleType;
import com.whfc.base.param.AlarmMessageParam;
import com.whfc.common.enums.WorkState;
import com.whfc.common.util.DateUtil;
import com.whfc.mach.dao.AppMachWarnRecordMapper;
import com.whfc.mach.dto.warn.MachWarnCheckDTO;
import com.whfc.mach.dto.warn.MachWarnRuleDTO;
import com.whfc.mach.entity.AppMachWarnRecord;
import com.whfc.mach.enums.AppMachWarnRuleType;
import com.whfc.mach.manager.warn.AbstractWarnManager;
import com.whfc.mach.manager.warn.AppWarnManager;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 设备工作状态报警规则
 *
 * @author guodong_sun
 */
@Service(value = "machWorkStateWarnCheckManagerImpl")
public class MachWorkStateWarnCheckManagerImpl extends AbstractWarnManager implements AppWarnManager {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String IDLE_WARN_CONTENT = "%s %s 在 %s,怠机时长>%s分钟,请您及时处理";
    private static final String WORK_WARN_CONTENT = "%s %s 在 %s,工作时长>%s分钟,请您及时处理";
    private static final String STOP_WARN_CONTENT = "%s %s 在 %s,停机时长>%s分钟,请您及时处理";

    @Autowired
    private AppMachWarnRecordMapper appMachWarnRecordMapper;

    private static final String PARAM_NAME = "value";

    @Override
    public boolean checkValue(MachWarnCheckDTO machWarnCheckDTO, MachWarnRuleDTO machWarnRuleDTO) {
        logger.info("检测设备工作状态报警规则,machWarnCheckDTO:{},machWarnRuleDTO:{}", machWarnCheckDTO.toString(), machWarnRuleDTO.toString());
        Integer workState = machWarnCheckDTO.getWorkState();
        if (AppMachWarnRuleType.IDLE.value().equals(machWarnRuleDTO.getRuleType())) {
            if (!WorkState.IDLE.getValue().equals(workState)) {
                return false;
            }
        }
        if (AppMachWarnRuleType.WORK.value().equals(machWarnRuleDTO.getRuleType())) {
            if (!WorkState.RUN.getValue().equals(workState)) {
                return false;
            }
        }
        if (AppMachWarnRuleType.STOP.value().equals(machWarnRuleDTO.getRuleType())) {
            if (!WorkState.STOP.getValue().equals(workState)) {
                return false;
            }
        }

        Integer times = machWarnCheckDTO.getTimes();
        String ruleValueStr = JSON.parseObject(machWarnRuleDTO.getRuleParam()).getString(PARAM_NAME);
        if (StringUtils.isBlank(ruleValueStr)) {
            return false;
        }
        Integer ruleValue = Integer.valueOf(ruleValueStr);

        if (!ObjectUtils.allNotNull(times, workState, ruleValue)) {
            return false;
        }

        // 状态报警时长:分钟->秒
        int maxSeconds = ruleValue * 60;

        if (times > maxSeconds) {
            machWarnCheckDTO.setTriggerValue(String.valueOf(times));
            return true;
        }
        return false;
    }

    @Override
    public AlarmMessageParam generateWarnRecord(MachWarnCheckDTO machWarnCheckDTO, MachWarnRuleDTO machWarnRuleDTO) {
        String ruleValue = JSON.parseObject(machWarnRuleDTO.getRuleParam()).getString(PARAM_NAME);
        String triggerParam = JSON.toJSONString(machWarnCheckDTO);
        // 生成报警记录
        String objectId = String.valueOf(machWarnCheckDTO.getMachId());
        AppMachWarnRecord record = new AppMachWarnRecord();
        Date time = machWarnCheckDTO.getTriggerTime();
        record.setDeptId(machWarnCheckDTO.getDeptId());
        record.setRuleType(machWarnRuleDTO.getRuleType());
        record.setRuleId(machWarnRuleDTO.getRuleId());
        record.setTriggerObjectId(objectId);
        record.setTriggerTime(time);
        record.setRuleExpress(machWarnRuleDTO.getRuleExpress());
        record.setRuleParam(machWarnRuleDTO.getRuleParam());
        record.setTriggerParam(triggerParam);
        appMachWarnRecordMapper.insertSelective(record);
        logger.info("生成设备工作状态报警,appAlarm:{}", record);

        // 组装生成消息参数
        AlarmMessageParam alarmMessageParam = this.restructMessageParam(record, machWarnCheckDTO.getMachTypeName(), machWarnCheckDTO.getMachCode(), machWarnCheckDTO.getWorkState(), ruleValue);
        return alarmMessageParam;
    }

    /**
     * 组合生成报警消息参数
     *
     * @param appAlarm
     * @param typeName
     * @param machCode
     * @return
     */
    private AlarmMessageParam restructMessageParam(AppMachWarnRecord appAlarm, String typeName, String machCode, Integer workState, String ruleValue) {

        String content = "";
        String tittle = "";
        if (workState.equals(WorkState.STOP.getValue())) {
            content = STOP_WARN_CONTENT;
            tittle = AppMachWarnRuleType.STOP.tittle();
        }

        if (workState.equals(WorkState.RUN.getValue())) {
            content = WORK_WARN_CONTENT;
            tittle = AppMachWarnRuleType.WORK.tittle();
        }

        if (workState.equals(WorkState.IDLE.getValue())) {
            content = IDLE_WARN_CONTENT;
            tittle = AppMachWarnRuleType.IDLE.tittle();
        }

        AlarmMessageParam alarmMessageParam = new AlarmMessageParam();
        String contentDetail = String.format(content, typeName, machCode, DateUtil.formatDateTime(appAlarm.getTriggerTime()), ruleValue);
        alarmMessageParam.setAlarmId(String.valueOf(appAlarm.getId()));
        alarmMessageParam.setDeptId(appAlarm.getDeptId());
        alarmMessageParam.setContent(contentDetail);
        alarmMessageParam.setModuleType(AppModuleType.MACH.value());
        alarmMessageParam.setTime(appAlarm.getTriggerTime());
        alarmMessageParam.setRuleId(appAlarm.getRuleId());
        alarmMessageParam.setTitle(tittle);
        alarmMessageParam.setTriggerObjectId(appAlarm.getTriggerObjectId());
        alarmMessageParam.setRuleType(appAlarm.getRuleType());
        return alarmMessageParam;
    }


}
